﻿using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using EnsureThat;

namespace Commons.Zip
{
	/// <summary>
	/// Extends zip files.
	/// </summary>
	public static class Zip
	{
		/// <summary>
		/// Writes a zip archive to a new file, asynchronously
		/// </summary>
		public static async Task<ZipArchive> SaveAsync(this ZipArchive archive, string file)
		{
			Ensure.That(archive, "archive").IsNotNull();
			Ensure.That(file, "file").IsNotNullOrWhiteSpace();
			ZipArchive newArchive = ZipFile.Open(file, ZipArchiveMode.Create);
			foreach (var entry in archive.Entries)
				await entry.Open().CopyToAsync(newArchive.CreateEntry(entry.FullName).Open());
			return newArchive;
		}
	}
}